# Setup system timezone
sudo -E timedatectl set-timezone Asia/Shanghai
# Setting the .bashrc for user vagrant(can't append to ~/.bashrc, because it's root)
cat << "EOF" >>  /home/vagrant/.bashrc
# Begin of setting ssh-agent daemon
agentEnv=~/.ssh/agent.env
agent_load_env(){
  test -f "$agentEnv" && . "$agentEnv" >| /dev/null;
}

agent_start(){
  (umask 077; ssh-agent >| "$agentEnv")
  . "$agentEnv" >| /dev/null;
}

agent_add_vagrant_insecure_key(){
  ssh-add /vagrant/insecure_private_key
}

agent_load_env

# agent_run_state: 0=agent running with key; 1=agent running without key; 2=agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
  agent_start
  agent_add_vagrant_insecure_key
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
  agent_add_vagrant_insecure_key
fi
unset agentEnv
# End of Setting ssh-agent daemon
EOF

# Back sources.list
sudo -E cp -f /etc/apt/sources.list /etc/apt/sources.list.bak
# remove sources.list
sudo -E rm /etc/apt/sources.list
# create new sources.list
cat << "EOF" >> /etc/apt/sources.list    
deb https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF

# Installing ansible
sudo -E apt update
sudo -E apt --yes install software-properties-common
sudo -E apt-add-repository --yes --update ppa:ansible/ansible
sudo -E apt --yes install ansible